package com.smfx.yxt.controller.sys.kccs;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.KccsCourseQueryVO;
import com.smfx.yxt.service.IKccsCourseInfoService;
import com.smfx.yxt.utils.ExcelExportUtil;
import com.smfx.yxt.utils.ExportSetInfoUtil;
import com.smfx.yxt.view.KccsCourseStudentSignTotal;

@RequestMapping("sys/course/studentSign")
@Controller
public class SysCourseStudentSignController extends BaseController {

	@Autowired
	private IKccsCourseInfoService kccsCourseInfoService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/kccs/courseStudentSign";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(KccsCourseQueryVO vo){
		try {
			if(vo.getTermId() == null) {
				vo.setTermId(getCurrentTerm().getId());
			}
			Page<KccsCourseStudentSignTotal> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<OrderBy> orders = new ArrayList<>();
			orders.add(new OrderBy("courseType"));
			orders.add(new OrderBy("courseNo"));
			orders.add(new OrderBy("grade","desc"));
			orders.add(new OrderBy("clazz"));
			orders.add(new OrderBy("studentNum"));
			page.setSorts(orders);
			List<KccsCourseStudentSignTotal> list = kccsCourseInfoService.selectStudentSignListByPage(page);
			Grid grid = new Grid();
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("export")
	ModelAndView workSignExport(KccsCourseQueryVO vo) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			if(vo.getTermId() == null) {
				vo.setTermId(getCurrentTerm().getId());
			}
			Page<KccsCourseStudentSignTotal> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(Integer.MAX_VALUE - 1);
			page.setParams(vo);
			List<OrderBy> orders = new ArrayList<>();
			orders.add(new OrderBy("courseType"));
			orders.add(new OrderBy("courseNo"));
			orders.add(new OrderBy("grade","desc"));
			orders.add(new OrderBy("clazz"));
			page.setSorts(orders);
			List<KccsCourseStudentSignTotal> list = kccsCourseInfoService.selectStudentSignListByPage(page);
			//数据源
			LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
			String title="校本课程学生报名表";
			map.put(title, list);
			
			//标题  和  标题变量
		    List<String[]> headNames = new ArrayList<String[]>();
		    headNames.add(new String[] { "课程分类","课程编号","课程名称","辅导老师","上课时间","学生班级","学生学号","学生姓名"});
		    List<String[]> fieldNames = new ArrayList<String[]>();
		    fieldNames.add(new String[] { "courseType","courseNo","courseName","classTeacher","signClassDate","gradeClazz","studentNum","studentName"});
		    
		    //excel生成帮助类
		    List<Integer[]> columnFormat=new ArrayList<Integer[]>();
		    Integer [] format=new Integer[22];
		    columnFormat.add(format);
		    
		    String [] titles=new String[] {title};
		    //excel生成帮助类
		    ExportSetInfoUtil setInfo = new ExportSetInfoUtil(map,titles,headNames,fieldNames,columnFormat);
		    request.setAttribute("exportDatas", setInfo);
		    
		    //生成
			modelAndView=new ModelAndView(new ExcelExportUtil(title+".xls",""));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	
}
